const changeShow = (tar, boolean) => {
  var pages = getCurrentPages()
  pages[pages.length - 1].setData({
    [tar]: !boolean
  })
}
// 格式化时间戳
function intervalTime(time, that, times) {
  that.data.interval = setInterval(function() {
    var {
      hrStr,
      minStr,
      secStr,
      deviation,
      day,
      shengSR
    } = getTimeminus(time)
    deviation--;
    if (deviation < 0) {
      clearInterval(that.data.interval);
      that.setData({
        shengSR: '00',
        hrStr: '00',
        minStr: '00',
        secStr: '00',
        day: '00',
      })

    } else {
      that.setData({
        shengSR,
        hrStr,
        minStr,
        secStr,
        deviation,
        day
      });
    }
  }.bind(that), times);
  return
}

function utilUrl(parse) {
  return new Promise((resolve, reject) => {
    wx.request({
      url: getApp().globalData.URL + 'wechat/Authorize/sunCode',
      method: 'POST',
      data: {
        app_id: getApp().globalData.app_id,
        scene: parse
      },
      success: (res) => {
        var url = getApp().globalData.imgUrls + res.data.data;
        wx.getImageInfo({
          src: url,
          success: resolve,
          fail: reject,
        })
      }
    })
  })
}
function getTimeDian(current) {
  var t, y, m, d, h, i, s;
  currentTimeNum = current ? new Date(current * 1000) : new Date()
  y = currentTimeNum.getFullYear() // 年
  m = currentTimeNum.getMonth() + 1 // 月
  d = currentTimeNum.getDate() // 日
  h = currentTimeNum.getHours() // 时
  i = currentTimeNum.getMinutes() // 分
  s = currentTimeNum.getSeconds() // 秒
  if ((h + "").length == 1) h = '0' + h;
  if ((i + "").length == 1) i = '0' + i;
  if ((s + "").length == 1) s = '0' + s;
  return {
    y,
    m,
    d,
    h,
    i,
    s
  }
}

function getTimeminus(current) {
  var deviation, day, hrStr, minStr, secStr, shengSR;
  currentTimeNum = Date.parse(new Date());
  deviation = current - currentTimeNum / 1000;
  //时间差(s)
  day = Math.floor(deviation / 3600 / 24);
  // 换算成天
  hr = Math.floor((deviation) / 3600);
  // 换算成小时
  shengSR = Math.floor(hr % 24);
  // 换算成剩余 小时
  min = Math.floor(((deviation) / 60) % 60);
   // 换算成剩余分
  sec = deviation % 60;
  // 换算成剩余秒

  hrStr = hr.toString();
  minStr = min.toString();
  secStr = sec.toString();
  shengSR = shengSR.toString();
  day = day.toString();
  if (minStr.length == 1) minStr = '0' + minStr;
  if (hrStr.length == 1) hrStr = '0' + hrStr;
  if (secStr.length == 1) secStr = '0' + secStr;
  if (shengSR.length == 1) shengSR = '0' + shengSR;
  if (day.length == 1) day = '0' + day;

  return {
    shengSR,
    hr,
    deviation,
    day,
    hrStr,
    minStr,
    secStr,
  }
}

function themes() {
  var getApps = getApp().globalData
  this.color = getApps.bgColors ? `color:${getApps.bgColors};` : 'color:#ff3e3e;';
  this.backgroundColor = getApps.bgColors ?
    `background-color:${getApps.bgColors};` :
    'background-color:#ff3e3e;';
  this.borderColor = getApps.bgColors ?
    `border-color:${getApps.bgColors};` :
    'border-color:#ff3e3e;'
}

function login(name, ing) {
  return new Promise(function(resolve, reject) {
    wx.login({
      success: function(res) {
        wx.request({
          url: getApp().globalData.URL + 'wechat/authorize/xcx_login',
          data: {
            app_id: getApp().globalData.app_id,
            jscode: res.code
          },
          success(res) {
            let {
              code,
              data,
              msg
            } = res.data
            if (code === 200) {
              resolve(data)
              var getApps = getApp().globalData;
              getApps.member_id = data.member_id;
              getApps.loginData = data
              // if (name) {
              //   API('wechat/authorize/updateapp', {
              //     member_id: getApps.member_id,
              //     nick_name: 'name',
              //     cover: 'https://oss.ztwlxx.cn/94557201905301141364917.png'
              //   })
              // }
            } else {
              wx.showToast({
                title: '系统繁忙',
                icon: 'none'
              })
            }
          }

        })
      }
    })
  })
}

function API(url, data, contentType, ) {

  var that = this
    wx.getNetworkType({
      success: function (res) {
        if (res.networkType != '4g' && res.networkType != 'wifi') {
          wx.showModal({
            title: '提示',
            content: `请检查网络连接`,
            success(res) {
              if (res.confirm) {
              }
            }
          })
        }
      }
    })
  
  var getApps = getApp().globalData
  var HOST_URI = getApps.URL
  wx.showNavigationBarLoading()
  return new Promise(function(resolve, reject) {
    let jsons = {
      app_id: getApps.app_id,
      member_id: getApps.member_id
    }
    data = Object.assign({}, jsons, data)
    url = HOST_URI + url
    if (!getApps.member_id) {
      login().then(ress => {
        data.member_id = getApps.member_id
        request(url, data, contentType).then(resRequ => {
          resolve(resRequ)
        })
      })
    } else {
      request(url, data, contentType).then(resRequ => {
        resolve(resRequ)
      })
    }
  })
}

function request(url, data, contentType) {
  return new Promise(function(resolve, reject) {
    wx.request({
      url,
      data,
      header: {
        'content-type': contentType || 'application/json'
      },
      method: 'POST',
      dataType: 'json',
      responseType: 'text',
      success(res) {
        wx.hideNavigationBarLoading()
        resolve(res.data)
      },
      fail(res) {
        wx.showToast({
          title:'请检查网络连接',
          duration: 5000,
          icon: 'none'
        })
      
      }
    })
  })
}
function Page(num){
  var pages = getCurrentPages();
  var Page = pages[pages.length - num];

  return Page
}
module.exports = {
  API,
  Page,
  themes,
  login: login,
  getTimeDian,
  getTimeminus,
  intervalTime,
  changeShow,
  utilUrl
}